<?php
class CartComponent extends Component {
	
	public $components = array (
			'Session' 
	);
	/**
	 * free shipping
	 */
	var $shipping_free = 0;
	/**
	 * internation shipping
	 */
	var $shipping_international = 20;
	/**
	 * national shipping
	 */
	var $shipping_domestic = 15; // default
	
	public $controller;
	
	public function __construct(ComponentCollection $collection, $settings = array()) {
		$this->controller = $collection->getController ();
		parent::__construct ( $collection, array_merge ( $this->settings, ( array ) $settings ) );
	}
	
	public function startup(Controller $controller) {
		// $this->controller = $controller;
	}
	
	public $maxQuantity = 99;
	
	public function add($id, $quantity = 1) {
		// $this->log("id".$id." qu".$quantity,"qu");
		if (! is_numeric ( $quantity )) {
			$quantity = 1;
		}
		
		$quantity = abs ( $quantity );
		
		if ($quantity > $this->maxQuantity) {
			$quantity = $this->maxQuantity;
		}
		
		if ($quantity == 0) {
			$this->remove ( $id );
			return;
		}
		
		$item = $this->controller->Item->find ( 'first', array (
				'recursive' => - 1,
				'conditions' => array (
						'Item.id' => $id 
				) 
		) );
		if (empty ( $item )) {
			return false;
		}
		
		$data ['item_id'] = $item ['Item'] ['id'];
		$data ['name'] = $item ['Item'] ['name'];
		$data ['weight'] = $quantity;
		$data ['price'] = $item ['Item'] ['original_price'];
		$data ['quantity'] = 1;
		$data ['subtotal'] = sprintf ( '%01.2f', $item ['Item'] ['original_price'] * $quantity );
		$data ['totalweight'] = sprintf ( '%01.2f', $quantity );
		$data ['Item'] = $item ['Item'];
		$this->Session->write ( 'Shop.OrderItem.' . $id, $data );
		$this->Session->write ( 'Shop.Order.shop', 1 );
		
		$this->Cart = ClassRegistry::init ( 'Cart' );
		
		$cartdata ['Cart'] ['sessionid'] = $this->Session->id ();
		$cartdata ['Cart'] ['quantity'] = 1;
		$cartdata ['Cart'] ['item_id'] = $item ['Item'] ['id'];
		$cartdata ['Cart'] ['name'] = $item ['Item'] ['name'];
		$cartdata ['Cart'] ['weight'] = $quantity;
		$cartdata ['Cart'] ['weight_total'] = $quantity;
		$cartdata ['Cart'] ['price'] = $item ['Item'] ['original_price'];
		$cartdata ['Cart'] ['subtotal'] = sprintf ( '%01.2f', $item ['Item'] ['original_price'] * $quantity );
		
		$existing = $this->Cart->find ( 'first', array (
				'recursive' => - 1,
				'conditions' => array (
						'Cart.sessionid' => $this->Session->id (),
						'Cart.item_id' => $item ['Item'] ['id'] 
				) 
		) );
		if ($existing) {
			$cartdata ['Cart'] ['id'] = $existing ['Cart'] ['id'];
		} else {
			$this->Cart->create ();
		}
		$this->Cart->save ( $cartdata, false );
		
		$this->cart ();
		
		return $item;
	}
	
	public function remove($id) {
		if ($this->Session->check ( 'Shop.OrderItem.' . $id )) {
			$item = $this->Session->read ( 'Shop.OrderItem.' . $id );
			$this->Session->delete ( 'Shop.OrderItem.' . $id );
			
			ClassRegistry::init ( 'Cart' )->deleteAll ( array (
					'Cart.sessionid' => $this->Session->id (),
					'Cart.item_id' => $id 
			), false );
			
			$this->cart ();
			return $item;
		}
		return false;
	}
	
	public function cart() {
		$shop = $this->Session->read ( 'Shop' );
		$quantity = 0;
		$weight = 0;
		$subtotal = 0;
		$total = 0;
		$order_item_count = 0;
		
		if (count ( $shop ['OrderItem'] ) > 0) {
			foreach ( $shop ['OrderItem'] as $item ) {
				$quantity += $item ['quantity'];
				$weight += $item ['totalweight'];
				$subtotal += $item ['subtotal'];
				$total += $item ['subtotal'];
				$order_item_count ++;
			}
			$d ['order_item_count'] = $order_item_count;
			$d ['quantity'] = $quantity;
			$d ['weight'] = sprintf ( '%01.2f', $weight );
			$d ['subtotal'] = sprintf ( '%01.2f', $subtotal );
			$shipping= $this->updateShipping ( null, $d );
			$d ['shipping'] =$shipping[0];
			$d ['shipping_type'] =$shipping[1];
			$total = $subtotal + $d ['shipping'];
			$d ['total'] = sprintf ( '%01.2f', $total );
			$this->Session->write ( 'Shop.Order', $d + $shop ['Order'] );
			return true;
		} else {
			$d ['quantity'] = 0;
			$d ['weight'] = 0;
			$d ['subtotal'] = 0;
			$d ['total'] = 0;
			$this->Session->write ( 'Shop.Order', $d + $shop ['Order'] );
			return false;
		}
	}
	public function updateShipping($val, $shop = array()) {
		if (empty ( $shop )) {
			$shipping = $val;
			// force to choose shipping cost
			$ammount = $this->getShipping ( $shipping );
			$shop = $this->Session->read ( 'Shop.Order' );
			// update shipping cost
			$shop ["shipping"] = $ammount;
			$shop ["shipping_type"] = $shipping;
			$shop ['total'] = sprintf ( '%01.2f', $shop ['subtotal'] + $ammount );
			$this->Session->write ( 'Shop.Order', $shop );
			$this->Session->write ( 'ShopShipping', $shipping );
		} else {
			$shipping = $this->Session->read ( 'ShopShipping' );
			// set shpping cost from cart page
			if ($shop ["subtotal"] >= 500) {
				$ammount = 0;
			} else {
				// if shipping cost already set
				if (! empty ( $shipping )) {
					$ammount = $this->getShipping ( $shipping );
				} else {
					$shipping = 1;
					// if not ,,nation :)
					$ammount = $this->getShipping ( $shipping );
					$this->Session->write ( 'ShopShipping', $shipping );
				}
			}
		}
		return array (
				$ammount,
				$shipping ,
				sprintf ( '%01.2f', $shop ['subtotal'] + $ammount ),
		);
	}
	private function getShipping($val) {
		switch ($val) {
			case 0 :
				$shipping = $this->shipping_free;
				break;
			case 1 :
				$shipping = $this->shipping_domestic;
				break;
			case 2 :
				$shipping = $this->shipping_international;
				break;
			
			default :
				$shipping = $this->shipping_domestic;
				break;
		}
		return $shipping;
	}
	public function clear() {
		ClassRegistry::init ( 'Cart' )->deleteAll ( array (
				'Cart.sessionid' => $this->Session->id () 
		), false );
		$this->Session->delete ( 'Shop' );
	}

}
